﻿using DFGK.IService;
using DFGK.IService.WCS;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Wcs;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DFGK_WMS.WebAPI.Controllers.WCS
{
    [Route("api/[controller]")]
    [ApiController]
    public class FromwcsController:BaseController
    {
        private readonly IWCSService wCSService;
        private readonly IInstructService instructService;
        private readonly WcsHttpClient wcsHttpClient;

        public FromwcsController(IWebHostEnvironment environment, IWCSService wCSService, IInstructService instructService,WcsHttpClient wcsHttpClient) :base(environment)
        {
            this.wCSService = wCSService;
            this.instructService = instructService;
            this.wcsHttpClient = wcsHttpClient;
        }
        /// <summary>
        /// 任务入库申请接口 
        /// </summary>
        /// <returns></returns>
        [Route("inware-task")]
        [HttpPost]
        public IActionResult InwareTask(TaskInRequest taskInRequest)
        {
            if (string.IsNullOrEmpty(taskInRequest.BarCode))
            {
                return Ok(new NewWcsApiRes()
                {
                    msgTime = DateTime.Now.ToString(),
                    returnInfo = "参数有误!",
                    returnStatus = 1
                });
            }
            var IsOk = wCSService.GetTaskByPallet(taskInRequest, out string Msg);
            if (IsOk)
            {
                return Ok(new NewWcsApiRes()
                {
                    msgTime = DateTime.Now.ToString(),
                    returnInfo = Msg,
                    returnStatus = 0
                });
            }
            return Ok(new NewWcsApiRes()
            {
                msgTime = DateTime.Now.ToString(),
                returnInfo = Msg,
                returnStatus = 1
            });


        }

        /// <summary>
        /// 任务状态上报 
        /// </summary>
        /// <param name="reportTaskData"></param>
        /// <returns></returns>
        [Route("task")]
        [HttpPost]
        public IActionResult ReportTask(ReportTaskData reportTaskData)
        {
            if (string.IsNullOrEmpty(reportTaskData.TaskId))
            {
                return Ok(new NewWcsApiRes()
                {
                    msgTime = DateTime.Now.ToString(),
                    returnInfo = "参数有误!",
                    returnStatus = 1
                });
            }
            var IsOk = wCSService.ReportTask(reportTaskData,out string Msg);
            if (IsOk)
            {
                return Ok(new NewWcsApiRes()
                {
                    msgTime = DateTime.Now.ToString(),
                    returnInfo = Msg,
                    returnStatus = 0
                });
            }
            return Ok(new NewWcsApiRes()
            {
                msgTime = DateTime.Now.ToString(),
                returnInfo = Msg,
                returnStatus = 1
            });
        }

        /// <summary>
        /// 任务取消接口 任务取消接口是让用户调用的
        /// </summary>
        /// <param name="palletCode"></param>
        /// <returns></returns>
        [Route("CancelTaskApi")]
        [HttpPost]
        public IActionResult CancelTaskApi(string palletCode)
        {
            var IsOk = wCSService.CancelTaskApi(palletCode,out string message);
            if (IsOk)
            {
                return Ok(BaseResult.Result(true, "取消成功！托盘码：" + palletCode, "200"));
            }
            return Ok(BaseResult.Result(true, "取消失败！托盘码：" + palletCode + message, "500"));
        }

        /// <summary>
        /// 修改任务终点位置  修改任务终点位置接口是让用户调用的
        /// </summary>
        /// <returns></returns>
        [Route("TaskChangeApi")]
        [HttpPost]
        public IActionResult TaskChangeApi(TaskChangeDTO taskChange)
        {
            var IsOk = instructService.WmsTaskChangeApi(taskChange, out string message);
            if (IsOk)
            {
                return Ok(BaseResult.Result(true, "修改任务位置成功!" , "200"));
            }
            return Ok(BaseResult.Result(true, "修改任务位置失败！" + message, "500"));
        }

        /// <summary>
        /// 下发任务测试接口
        /// </summary>
        /// <param name="taskAccept"></param>
        /// <returns></returns>
        [Route("TaskAcceptApi")]
        [HttpPost]
        public IActionResult TaskAcceptApi(TaskAccept taskAccept)
        {
            var result = wcsHttpClient.TaskAcceptApi(taskAccept);
            if (result.returnStatus == 0)
            {
                return Ok(BaseResult.Result(true, "下发成功!", "200"));
            }
            return Ok(BaseResult.Result(true, "下发失败！" + result.returnInfo, "500"));
        }
    }
}
